<?php
namespace app\appoint\model;
use app\BaseModel;
use think\facade\Db;



class QuestionText extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_question_text';


    /**
     * @author chenniang
     * @DataTime: 2021-01-26 16:12
     * @功能说明:验证一下跳转的id
     */
    public function getJumpIdAttr($value,$data){

        if(!empty($data['jump_id'])&&!empty($data['is_jump'])&&!empty($data['q_id'])){

            if($data['jump_id']==-1){

                return $data['jump_id'];
            }

            $list_model = new QuestionList();
            //当前题目的排序值
            $top   = $list_model->where(['id'=>$data['q_id']])->value('top');
            //需要跳转的题目的排序值
            $j_top = $list_model->where(['id'=>$data['jump_id']])->value('top');

            $id = $j_top<$top?$data['jump_id']:0;

            return $id;

        }

    }


    /**
     * @param $dis
     * @param $page
     * @功能说明:问卷列表
     * @author chenniang
     * @DataTime: 2021-01-25 15:56
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->paginate($page)->toArray();

        return $data;
    }

    /**
     * @param $dis
     * @功能说明:问卷下拉框
     * @author chenniang
     * @DataTime: 2021-01-25 15:56
     */
    public function dataSelect($dis){

        $data = $this->where($dis)->select()->toArray();

        return $data;
    }

    /**
     * @param $data
     * @功能说明:添加
     * @author chenniang
     * @DataTime: 2021-01-25 15:57
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;
    }

    /**
     * @param $dis
     * @param $data
     * @功能说明:编辑
     * @author chenniang
     * @DataTime: 2021-01-25 15:57
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }

    /**
     * @param $dis
     * @功能说明:详情
     * @author chenniang
     * @DataTime: 2021-01-25 15:57
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():$data;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-01-25 17:01
     * @功能说明:问题的答案详情
     */
    public function textInfo($data){

        $text = [];
        //矩阵单选题
        if($data['type']==1){

            $dis = [

                'uniacid' => $data['uniacid'],
                //头题的id
                'q_id'    => $data['id'],

                'type'    => 1,

                'q_type'  => 1,
                //用户提交文券的id
                'text_id' => $data['text_id']
            ];
            //查询所有问题
            $text = $this->dataSelect($dis);

            foreach ($text as $k=>$value){

                $dis = [

                    'uniacid' => $data['uniacid'],

                    'pid'     => $value['id'],

                    'type'    => 2,

                    'q_type'  => 1
                ];

                $text[$k]['ans'] = $this->dataSelect($dis);
            }

        }
        //联动单选题
        if($data['type']==2){

            $dis = [

                'uniacid' => $data['uniacid'],

                'q_id'    => $data['id'],

                'type'    => 2,

                'q_type'  => 2,
                //用户提交文券的id
                'text_id' => $data['text_id']
            ];
            //直接查答案
            $text = $this->dataSelect($dis);

        }

        return $text;

    }




}